import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import myCookies from '@/utils/myCookies';
const token = myCookies.getToken();
export const useCounterStore = defineStore('counter', () => {
  // 记住账号密码
  const rememberUserAndPass = ref(false)
  //控制菜单收缩
  const isCollapse = ref(false)
  const collapseMenu = () => {
    isCollapse.value = !isCollapse.value
  }
  //面包屑
  interface tabsData {
    path: string,
    name: string,
  }
  const tabsList = ref<tabsData[]>([{
    path: 'home',
    name: '首页',
  }])
  const selectMenu = (val: tabsData) => {
    //判断添加的数据是否为首页
    if (val.path !== "home") {
      //返回索引
      const index = tabsList.value.findIndex((item: any) => item.name === val.name)
      // console.log(tabsList,val)
      //如果不存在侧添加
      if (index === -1) {
        tabsList.value.push(val)
      }
    }
  }
  return { isCollapse, collapseMenu, tabsList, selectMenu, rememberUserAndPass }
})
export const useMessage = defineStore('message', () => {
  interface Message {
    role: string;
    id: any;
    createAt: number;
    content: any;
  }
  interface Session {
    sessionId: string;
    userId: number;
    id: number;
    nickname: string
  }
  //获取用户信息
  const userInfoItem = localStorage.getItem("userInfo")
  const userInfo = ref()
  const editableTabs = ref<Session[]>([])
  if (userInfoItem != null) {
    userInfo.value = JSON.parse(userInfoItem)
  }

  const message = ref<Message[]
  >([])

  const userId = ref<Number>()
  const roleInfo = ref({
    user: {
      name: userInfo.value.nickname,
      avatar: userInfo.value.avatar
    },
    duifang: {
      name: '用户' + userId.value,
      avatar: 'https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/other/logo.png'
    },
  })
  return { editableTabs, userId, roleInfo, message }
})
